<template>
  <div class="newsDetail">
    <h3 class="tit">{{detailList.title}}</h3>
    <div class="info">
      <van-row>
        <van-col span="10">发表时间 : {{detailList.add_time}}</van-col>
        <van-col span="6" offset="8">点击次数 ：{{detailList.click}}</van-col>
      </van-row>
    </div>
    <div v-html="detailList.content" class="footer"></div>
    <h4>发表评论</h4>
    <hr>
    <van-cell-group>
      <van-field v-model="value" rows="3" autosize type="textarea" />
    </van-cell-group>
    <van-button type="info" :block="true" @click="addComments">发表评论</van-button>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="(item,index) in commentsList" :key="index">
        <template slot="title">
          <div class="title">
            <span> 第{{index+1}}楼</span>
            <span>用户 : {{item.user_name}}</span>
            <span>发表时间 : {{item.add_time}}</span>
          </div>
        </template>
        <template slot="label">
          <p>{{item.content}}</p>
        </template>
      </van-cell>
    </van-list>
    <van-button plain type="warning" :block="true" @click="onloadComment">加载更多</van-button>
  </div>
</template>

<script>
export default {
  name: 'detail',
  data () {
    return {
      detailList: {
        title: '',
        click: 0,
        content: '',
        add_time: ''
      },
      detailId: '',
      value: '',
      commentsList: [],
      loading: false,
      finished: false,
      pageindex: 1
    }
  },
  created () {
    this.getDetailList()
    this.getComments()
  },
  methods: {
    async getDetailList () {
      const id = this.$route.params.id
      this.detailId = id
      const { data: res } = await this.$http.get(`/api/getnew/${id}`)
      console.log(res)
      if (res.status !== 0) return this.Notify({ type: 'danger', message: '获取新闻详情数据失败' })
      res.message.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD')
      })
      this.detailList = res.message[0]
    },
    async addComments () {
      console.log(this.value)
      const { data: res } = await this.$http.post(`/api/postcomment/${this.detailId}`, this.$qs.stringify({
        content: this.value
      }))
      console.log(res)
      this.value = ''
      this.getComments()
    },
    async getComments () {
      this.loading = false
      const { data: res } = await this.$http.get(`/api/getcomments/${this.detailId}`, {
        params: {
          pageindex: this.pageindex
        }
      })
      console.log(res)
      res.message.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD')
      })
      this.total = res.message.length
      this.commentsList = res.message
    },
    onLoad () {
      this.loading = false
      // if (this.commentsList.length >= 40) {
      //   this.finished = true
      // }
    },
    async onloadComment () {
      this.loading = false
      var click = this.pageindex
      click++
      const { data: res } = await this.$http.get(`/api/getcomments/${this.detailId}`, {
        params: {
          pageindex: click
        }
      })
      console.log(res)
      res.message.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD')
      })
      if (res.message) {
        res.message.forEach(item => {
          this.commentsList.push(item)
        })
      } else {
        this.finished = true
        this.pageindex = 1
      }
    }
  }
}
</script>

<style lang="less" scoped>
.newsDetail {
  text-align: left;
  margin-bottom: 50px;
  padding: 0 10px;
}
.tit {
  color: #1989fa;
  font-size: 14px;
  text-align: center;
  line-height: 30px;
  margin: 0;
  padding: 5px 0;
}
.info {
  font-size: 13px;
  margin: 10px 0;
  color: #1989fa;
}

.footer {
  text-align: left;
}
.van-list {
  padding-top: 5px;
  color: #323233;
}
.van-cell-group {
  border: 1px solid #ccc;
  margin-bottom: 5px;
}
/deep/ .van-cell {
  padding: 0;
}
.title {
  background-color: #ccc;
}
.title span {
  padding: 0 5px;
}
.van-cell__label {
  background-color: #fff;
  line-height: 35px;
  text-indent: 2em;
}
p {
  margin: 0;
}
</style>
